{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cdcd64ad-3dfd-43c1-ba4e-31e90e0803d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def lazy_sum(*args): \n",
    "    def sum(): \n",
    "        ax = 0\n",
    "        for n in args: \n",
    "            ax = ax + n \n",
    "        return ax \n",
    "    return sum \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "03d3a92b-4b17-4975-99b4-d7594bfdac33",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = lazy_sum(1, 3, 5, 7 , 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "db240a84-e98c-439e-8064-193ed373b3f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.lazy_sum.<locals>.sum()>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "84153389-d285-4762-be08-1c2ca670e44b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2e30c73b-a06d-4251-ab54-74f1a77313c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def inc(): \n",
    "    x = 0\n",
    "    def fn(): \n",
    "        x = x + 1\n",
    "        return x \n",
    "    return fn \n",
    "f = inc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "57fcc6ea-c3ae-4197-af98-e99a3b352330",
   "metadata": {},
   "outputs": [
    {
     "ename": "UnboundLocalError",
     "evalue": "local variable 'x' referenced before assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnboundLocalError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m)\n",
      "Cell \u001b[1;32mIn[1], line 4\u001b[0m, in \u001b[0;36minc.<locals>.fn\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfn\u001b[39m(): \n\u001b[1;32m----> 4\u001b[0m     x \u001b[38;5;241m=\u001b[39m \u001b[43mx\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m      5\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m x\n",
      "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'x' referenced before assignment"
     ]
    }
   ],
   "source": [
    "print(f())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3ef340fc-66e2-4a6c-ac7d-0cd29a958bd1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def inc(): \n",
    "    x = 0\n",
    "    def fn(): \n",
    "        nonlocal x \n",
    "        x = x + 1\n",
    "        return x \n",
    "    return fn \n",
    "f = inc()\n",
    "print(f())\n",
    "f()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bbcf8ac2-c63e-4ea0-b817-89bc5f2966ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "def createCounter():\n",
    "    x = 0\n",
    "    def counter(): \n",
    "        nonlocal x \n",
    "        x = x + 1\n",
    "        return x \n",
    "    return counter\n",
    "fun = createCounter()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4dca9fa1-e6b7-4170-85be-71aae9d6b164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fun() \n",
    "fun()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8d67ad3e-55bb-4363-9d7a-aafb27245fac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fun()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "30d244f8-1ce1-4c2e-a9d3-3d73b2d30067",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x: x * x , [1, 2,3,4,5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a9a25bc0-4684-42c5-b3ee-6ef6f8763c6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = lambda x: x * x "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6da8d4dd-58d2-459f-b35c-17dd54d96821",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(x)>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7b85f584-8b9a-4456-9da0-fbf43f7929c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "83c097bf-d7a7-4065-8f1f-7c291a314983",
   "metadata": {},
   "outputs": [],
   "source": [
    " L = list(filter(lambda x: x%2 ==1 , range(1,20)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7de72ce1-9036-422d-8d17-2ac04f96ac15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n"
     ]
    }
   ],
   "source": [
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a51b09e4-8fcc-4a26-8b23-89ca528031f0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
